package com.example.shiwu.util;

import com.google.firebase.crashlytics.buildtools.reloc.com.google.common.primitives.Ints;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

public class ByteUtils {
    private static final char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};



    public static void main(String[] args) {
        System.out.println(revertHex("ABCDDEFH"));
    }

    public static byte[] hexToBytes(String hex) {
        if (hex == null || hex.length() == 0) {
            return null;
        }
        char[] chars = hex.toCharArray();
        byte[] bytes = new byte[chars.length / 2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt("" + chars[i * 2] + chars[i * 2 + 1], 16);
        }
        return bytes;
    }


    public static String bytesToHex(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        StringBuilder hex = new StringBuilder();

        for (byte b : bytes) {
            hex.append(hexDigits[(b >> 4) & 0x0F]);
            hex.append(hexDigits[b & 0x0F]);
        }
        return hex.toString();

    }

    /**
     * 反转
     *
     * @param source
     * @param begin
     * @param length
     * @return
     */
    public static byte[] arraySliceAndReverse(
            byte[] source,
            int begin,
            int length) {
        byte[] tempArr = new byte[source.length];
        System.arraycopy(source, 0, tempArr, 0, source.length);
        byte temp;
        for (int i = 0; i < (source.length - length) / 2; i++) {
            temp = tempArr[begin + i];
            tempArr[begin + i] = tempArr[source.length - i - 1];
            tempArr[source.length - i - 1] = temp;
        }
        return tempArr;
    }


    /**
     * 已测试
     *
     * @param bytes
     * @return
     */
    public static int bytesToInt(byte[] bytes) {
        byte[] temp = new byte[Integer.BYTES];
        if (bytes.length < 4) {
            System.arraycopy(bytes, 0, temp, temp.length - bytes.length, bytes.length);
        } else if (bytes.length > 4) {
            System.arraycopy(bytes, bytes.length - 4, temp, 0, Integer.BYTES);
        }
        return Ints.fromByteArray(temp);
    }

    public static String bytesToString(byte[] bytes, char placeholder) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            if (b == (byte) placeholder) {
                break;
            }
            sb.append((char) b);
        }
        return sb.toString();
    }

    /**
     * String -> bytes array
     *
     * @param param       原字符串
     * @param length      bytes 数组长度
     * @param placeholder 数组空余位置填充
     * @return 字节数组
     */
    public static byte[] stringToBytes(String param, int length, char placeholder) {
        byte[] rawContent = new byte[length];
        char[] array = param.toCharArray();
        int len = Math.min(array.length, length);
        for (int i = 0; i < len; i++) {
            rawContent[i] = (byte) array[i];
        }
        for (int i = array.length; i < length; i++) {
            rawContent[i] = (byte) (placeholder);
        }
        return rawContent;
    }

    public static String revertHex(String hex) {
        StringBuilder sb = new StringBuilder();
        for (int i = hex.length() - 1; i >= 0; i -= 2) {
            sb.append(hex, i - 1, i + 1);
        }
        return sb.toString();
    }

    public static float[] byteArrToFloatArr(byte[] bytes) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
        float[] aFloat = new float[floatBuffer.limit()];
        floatBuffer.get(aFloat);
        return aFloat;
    }

    public static byte[] floatArrToByteArr(float[] floats) {
        ByteBuffer buffer = ByteBuffer.allocate(floats.length * 4);
        FloatBuffer floatBuffer = buffer.asFloatBuffer();
        floatBuffer.put(floats);
        return buffer.array();
    }
}
